﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;

namespace WorldOnFire
{
    public class CFlak : ABCProjectile
    {
        public override void Initialize(int _nObjectCount)
        {
            // Initialize the base members
            base.Initialize(_nObjectCount);

            // Set the enumerations
            eBaseType[] eBaseTypes = new eBaseType[] { eBaseType.MOVING_OBJECT, eBaseType.PROJECTILE };
            this.SetBaseTypeID(eBaseTypes);

            this.SetObjectType(eObjectType.PROJECTILE_FLAK);
            this.SetProjectileID(eProjectileType.FLAK);

            // Assign collidable types
            m_CollidableTypeList.Add(eObjectType.ENEMY_BOMBER);
            m_CollidableTypeList.Add(eObjectType.ENEMY_DETONATOR);
            m_CollidableTypeList.Add(eObjectType.ENEMY_GUARDIAN);
            m_CollidableTypeList.Add(eObjectType.ENEMY_FRIGATE);
            m_CollidableTypeList.Add(eObjectType.ENEMY_DESTROYER);
            m_CollidableTypeList.Add(eObjectType.ENEMY_CRUISER);
            m_CollidableTypeList.Add(eObjectType.ENEMY_BATTLESHIP);
            m_CollidableTypeList.Add(eObjectType.ENEMY_CARRIER);
            m_CollidableTypeList.Add(eObjectType.ENEMY_JUGGERNAUT);
        }

        protected override void InitializeProjectile(int _nProjectileIndex, ABCTurret _Turret, int _nTurretIndex, Vector3 _vOffset)
        {
            // Initialize the base members
            base.InitializeProjectile(_nProjectileIndex, _Turret, _nTurretIndex, _vOffset);

            // Re-calculate the lifetime of the projectile to explode close to the target
            // (Time = Distance To Target / Speed)
            ABCEnemy Enemy = s_ObjectManager.GetEnemy(_Turret.GetTargetType(_nTurretIndex));
            int nEnemyIndex = _Turret.GetTargetIndex(_nTurretIndex);

            // Find the projected position
            Vector3 vProjectedPosition = Enemy.CalculateProjectedPosition(nEnemyIndex, m_fMaximumTime[_nProjectileIndex]);

            // Calculate the new time
            Vector3 vBetween = vProjectedPosition - m_mTransform[_nProjectileIndex].Translation;
            m_fMaximumTime[_nProjectileIndex] = vBetween.Length() / m_fSpeed[_nProjectileIndex];
        }

        protected override void KillProjectile(int _nProjectileIndex)
        {
            // Set the projectile to inactive
            base.KillProjectile(_nProjectileIndex);

            // Create an explosion
            SEventSystem.SendEvent( new CEventObjectExplosion(
                m_mTransform[_nProjectileIndex].Translation, 
                m_fAreaOfEffect[_nProjectileIndex], 
                m_fDamage[_nProjectileIndex]));
        }
    }
}
